<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
                    <el-form-item :label="$locale('myDevice.determinedList.turnover box')" prop="boxName">
                        <el-input v-model="queryParams.boxName" :placeholder="$locale('myDevice.determinedList.Please enter the turnover box')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <!-- <el-form-item label="周转箱编号" prop="boxNum">
                        <el-input v-model="queryParams.boxNum" placeholder="请输入周转箱编号" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item> -->
                    <el-form-item :label="$locale('geWudevice.Equipment number')" prop="deviceNum">
                        <el-input v-model="queryParams.deviceNum" :placeholder="$locale('myDevice.determinedList.Please enter the device number')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.Equipment name')" prop="deviceName">
                        <el-input v-model="queryParams.deviceName" :placeholder="$locale('geWudevice.Please enter the device name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">{{ $locale('classList.add.added') }}</el-button>
                      <el-button type="primary" icon="el-icon-download" size="mini" @click="handleExportExcel">{{ $locale('myDevice.determinedList.Export to Excel') }}</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="listData" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.device')" align="center" key="deviceName" prop="deviceName">
                        <template slot-scope="scope">
                            {{ scope.row.deviceName }}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.Equipment code')" align="center" key="deviceNum" prop="deviceNum" width="160"></el-table-column>
                    <el-table-column :label="$locale('geWudevice.quantity')" align="center" key="deviceTotal" prop="deviceTotal"></el-table-column>
                    <!-- <el-table-column label="状态" align="center" key="confirmStatus" prop="confirmStatus">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.confirmStatus?'success':'danger'" effect="dark">
                                {{ scope.row.confirmStatus?'已确定':'未确定' }}
                            </el-tag>
                        </template>
                    </el-table-column> -->
                    <el-table-column :label="$locale('myDevice.determinedList.CONFIRMED BY')" align="center" key="confirmBy" prop="confirmBy"></el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.Confirm time')" align="center" key="confirmTime" prop="confirmTime"></el-table-column>
                    <el-table-column :label="$locale('myDevice.determinedList.add.Equipment source')" align="center" key="deviceSource" prop="deviceSource"></el-table-column>
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button
                                v-if="(scope.row.deviceId == 0 && scope.row.isTurnoverBox) || (scope.row.parentId == 0 && !scope.row.isTurnoverBox)"
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)">
                              {{ $locale('classList.add.modify') }}
                            </el-button>
                            <el-button
                                v-if="(scope.row.deviceId == 0 && scope.row.isTurnoverBox) || (scope.row.parentId == 0 && !scope.row.isTurnoverBox)"
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="handleDelete(scope.row)">
                              {{ $locale('formMarkering.list.delete') }}
                            </el-button>
                            <el-button v-if="scope.row.upkeepStatus" size="mini" type="text" icon="el-icon-edit" @click="handleDeviceUpkeep(scope.row)">
                              {{ $locale('myDevice.determinedList.maintenance') }}</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <addTurnoverBox ref="addTurnoverBox" @getList="getList"></addTurnoverBox>
        <el-dialog :title="$locale('myDevice.determinedList.Please leave a message for repairing experimental equipment!')" :visible.sync="open" width="500px" :close-on-click-modal="false">
            <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" :placeholder="$locale('myDevice.determinedList.Please enter the content')" v-model="rowData.upkeepContent"></el-input>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">{{ $locale('classList.add.determine') }}</el-button>
                <el-button @click="open = false">{{ $locale('classList.add.Cancel') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { apiExportDevice, deleteById, schoolDevicePageV2 } from '@/api/myDevice';
import { add } from '@/api/device/deviceUpkeep';
import addTurnoverBox from './add/index.vue';
import { getNames } from '@/utils/index';

// 设备列表
export default {
    name: 'experiment',
    components: { addTurnoverBox },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            rowData: {
                upkeepContent: ''
            },
            // 用户表格数据
            listData: [],
            // 弹出层标题
            title: '',
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                boxName: '',
                deviceNum: '',
                deviceName: ''
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            schoolDevicePageV2(this.queryParams).then(({ data }) => {
                this.listData = data.records.map((item) => {
                    item.children = item.childVOS.map((i) => {
                        i.id = 'child' + i.id;
                        // i.deviceName = i.deviceName
                        return i;
                    });
                    return item;
                });
                this.total = data.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
      handleExportExcel() {
        // apiExportDevice(this.queryParams).then(res => {
        //   console.log(res);
        // })
        this.download('/manage/schoolDevice/exportSchoolDevice', {
          ...this.queryParams
        }, `导出的设备.xlsx`, {
          method: 'get',
        })
      },
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        /** 提交按钮 */
        submitForm: function () {
            add(this.rowData).then((response) => {
                this.$modal.msgSuccess(this.$locale('geWudevice.add.tip5'));
                this.open = false;
                this.getList();
            });
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$refs.addTurnoverBox.handleAdd(this.ids);
        },
        /** 维修按钮操作 */
        handleDeviceUpkeep(row) {
            this.open = true;
            this.rowData.upkeepContent = row.upkeepContent || '';
            const result = row.id ? this.replaceStr(row.id) : '';
            this.rowData.deviceId = row.deviceId;
            this.rowData.schoolDeviceId = result;
            this.rowData.deviceName = row.deviceName;
            this.rowData.deviceNum = row.deviceNum;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            row.id = row.id ? this.replaceStr(row.id) : '';
            this.$refs.addTurnoverBox.handleUpdate(row);
        },
        replaceStr(str, search = 'child') {
            str = str.toString();
            return str.includes(search) ? str.replace(search, '') : str;
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            row.id = row.id ? this.replaceStr(row.id) : '';
            const ids =
                row.id ||
                this.ids.map((item) => {
                    item = item ? this.replaceStr(item) : '';
                    return item;
                });
            this.$modal
                .confirm(this.$locale('geWudevice.tip1') + getNames(this.listData, ids, 'deviceName') + this.$locale('geWudevice.tip2'))
                .then(function () {
                    return deleteById({ id: ids });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$locale('geWudevice.tip3'));
                })
                .catch(() => {});
        }
    }
};
</script>
<style lang="scss" scoped>
::v-deep .el-table__row--level-1 .el-checkbox {
    display: none;
}
</style>
